package com.harmonyos.samples.bean.code;

import java.util.HashMap;
import java.util.Map;

public class ETSMethodInfo {

    //方法正则表达式，用于判断时方法定义还是方法调用
    public static final String METHOD_PATTERN = "^[a-zA-Z][a-zA-Z]*\\([a-zA-Z\\s,.:\\[\\]<]*\\):\\s*(void|Promise<.*>|boolean|[a-zA-Z<,>\\s|]]+)$";

    //插入代码时需要替换的文字
    public static final String DEFAULT_REPLACE = "##pluginInsert##";

    //没有返回值的方法体
    public static final String DEFAULT_METHOD_BLOCK = """
            {
              ##pluginInsert##
            }
            """;
    //有返回值的方法体
    public static final String DEFAULT_METHOD_BLOCK_RETURN = """
            {
              ##pluginInsert##
              return 0;
            }
            """;

    public static final String KEY_VOID = "void";
    public static final String KEY_RETURN = "return";
    public static final String METHOD_ON_CREATE = "onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void";
    public static final String METHOD_ON_WINDOW_STAGE_CREATE = "onWindowStageCreate(windowStage: window.WindowStage): void";
    public static final String METHOD_ON_WINDOW_STAGE_WILL_DESTROY = "onWindowStageWillDestroy(windowStage: window.WindowStage): void";
    public static final String METHOD_ON_WINDOW_STAGE_DESTROY = "onWindowStageDestroy(): void";
    public static final String METHOD_ON_WINDOW_STAGE_RESTORE = "onWindowStageRestore(windowStage: window.WindowStage): void";
    public static final String METHOD_ON_DESTROY = "onDestroy(): void | Promise<void>";
    public static final String METHOD_ON_FOREGROUND = "onForeground(): void";
    public static final String METHOD_ON_BACKGROUND = "onBackground(): void";
    public static final String METHOD_ON_CONTINUE = "onContinue(wantParam: Record<string, Object>): AbilityConstant.OnContinueResult | Promise<AbilityConstant.OnContinueResult>";
    public static final String METHOD_ON_NEW_WANT = "onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void";
    public static final String METHOD_ON_DUMP = "onDump(params: Array<string>): Array<string>";
    public static final String METHOD_ON_SAVE_STATE = "onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>): AbilityConstant.OnSaveResult";
    public static final String METHOD_ON_SHARE = "onShare(wantParam: Record<string, Object>): void";
    public static final String METHOD_ON_PREPARE_TO_TERMINATE = "onPrepareToTerminate(): boolean";
    public static final String METHOD_ON_BACK_PRESSED = "onBackPressed(): boolean";
    public static final String METHOD_ON_CONFIGURATION_UPDATE = "onConfigurationUpdate(newConfig: Configuration): void";
    public static final String METHOD_ON_MEMORY_LEVEL = "onMemoryLevel(level: AbilityConstant.MemoryLevel): void";

    public static final String METHOD_NAME_ON_CREATE = "onCreate";
    public static final String METHOD_NAME_ON_WINDOW_STAGE_CREATE = "onWindowStageCreate";
    public static final String METHOD_NAME_ON_WINDOW_STAGE_WILL_DESTROY = "onWindowStageWillDestroy";
    public static final String METHOD_NAME_ON_WINDOW_STAGE_DESTROY = "onWindowStageDestroy";
    public static final String METHOD_NAME_ON_WINDOW_STAGE_RESTORE = "onWindowStageRestore";
    public static final String METHOD_NAME_ON_DESTROY = "onDestroy";
    public static final String METHOD_NAME_ON_FOREGROUND = "onForeground";
    public static final String METHOD_NAME_ON_BACKGROUND = "onBackground";
    public static final String METHOD_NAME_ON_CONTINUE = "onContinue";
    public static final String METHOD_NAME_ON_NEW_WANT = "onNewWant";
    public static final String METHOD_NAME_ON_DUMP = "onDump";
    public static final String METHOD_NAME_ON_SAVE_STATE = "onSaveState";
    public static final String METHOD_NAME_ON_SHARE = "onShare";
    public static final String METHOD_NAME_ON_PREPARE_TO_TERMINATE = "onPrepareToTerminate";
    public static final String METHOD_NAME_ON_BACK_PRESSED = "onBackPressed";
    public static final String METHOD_NAME_ON_CONFIGURATION_UPDATE = "onConfigurationUpdate";
    public static final String METHOD_NAME_ON_MEMORY_LEVEL = "onMemoryLevel";

    public static final String METHOD_NAME_ABOUT_TO_APPEAR = "aboutToAppear";
    public static final String METHOD_NAME_ON_DID_BUILD = "onDidBuild";
    public static final String METHOD_NAME_ABOUT_TO_DISAPPEAR = "aboutToDisappear";
    public static final String METHOD_NAME_ON_PAGE_SHOW = "onPageShow";
    public static final String METHOD_NAME_ON_PAGE_HIDE = "onPageHide";
    public static final String METHOD_NAME_ON_BACK_PRESS = "onBackPress";
    public static final String METHOD_NAME_ABOUT_TO_REUSE = "aboutToReuse";
    public static final String METHOD_NAME_ABOUT_TO_RECYCLE = "aboutToRecycle";
    public static final String METHOD_NAME_ON_WILL_APPLY_THEME = "onWillApplyTheme";

    public static final String METHOD_ABOUT_TO_APPEAR = "aboutToAppear(): void";
    public static final String METHOD_ON_DID_BUILD = "onDidBuild(): void";
    public static final String METHOD_ABOUT_TO_DISAPPEAR = "aboutToDisappear(): void";
    public static final String METHOD_ON_PAGE_SHOW = "onPageShow(): void";
    public static final String METHOD_ON_PAGE_HIDE = "onPageHide(): void";
    public static final String METHOD_ON_BACK_PRESS = "onBackPress(): void | boolean";
    public static final String METHOD_ABOUT_TO_REUSE = "aboutToReuse(params: ESObject): void";
    public static final String METHOD_ABOUT_TO_RECYCLE = "aboutToRecycle(): void";
    public static final String METHOD_ON_WILL_APPLY_THEME = "onWillApplyTheme(theme: Theme): void";

    public static final Map<String, String> abilityMethodInfoMap = new HashMap<>();
    public static final Map<String, String> componentMethodInfoMap = new HashMap<>();

    public static void initAbilityMethodInfo() {
        abilityMethodInfoMap.put(METHOD_NAME_ON_CREATE, METHOD_ON_CREATE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_WINDOW_STAGE_CREATE, METHOD_ON_WINDOW_STAGE_CREATE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_WINDOW_STAGE_WILL_DESTROY, METHOD_ON_WINDOW_STAGE_WILL_DESTROY);
        abilityMethodInfoMap.put(METHOD_NAME_ON_WINDOW_STAGE_DESTROY, METHOD_ON_WINDOW_STAGE_DESTROY);
        abilityMethodInfoMap.put(METHOD_NAME_ON_WINDOW_STAGE_RESTORE, METHOD_ON_WINDOW_STAGE_RESTORE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_DESTROY, METHOD_ON_DESTROY);
        abilityMethodInfoMap.put(METHOD_NAME_ON_FOREGROUND, METHOD_ON_FOREGROUND);
        abilityMethodInfoMap.put(METHOD_NAME_ON_BACKGROUND, METHOD_ON_BACKGROUND);
        abilityMethodInfoMap.put(METHOD_NAME_ON_CONTINUE, METHOD_ON_CONTINUE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_NEW_WANT, METHOD_ON_NEW_WANT);
        abilityMethodInfoMap.put(METHOD_NAME_ON_DUMP, METHOD_ON_DUMP);
        abilityMethodInfoMap.put(METHOD_NAME_ON_SAVE_STATE, METHOD_ON_SAVE_STATE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_SHARE, METHOD_ON_SHARE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_PREPARE_TO_TERMINATE, METHOD_ON_PREPARE_TO_TERMINATE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_BACK_PRESSED, METHOD_ON_BACK_PRESSED);
        abilityMethodInfoMap.put(METHOD_NAME_ON_CONFIGURATION_UPDATE, METHOD_ON_CONFIGURATION_UPDATE);
        abilityMethodInfoMap.put(METHOD_NAME_ON_MEMORY_LEVEL, METHOD_ON_MEMORY_LEVEL);
    }

    public static void initComponentMethodInfo() {
        componentMethodInfoMap.put(METHOD_NAME_ABOUT_TO_APPEAR, METHOD_ABOUT_TO_APPEAR);
        componentMethodInfoMap.put(METHOD_NAME_ON_DID_BUILD, METHOD_ON_DID_BUILD);
        componentMethodInfoMap.put(METHOD_NAME_ABOUT_TO_DISAPPEAR, METHOD_ABOUT_TO_DISAPPEAR);
        componentMethodInfoMap.put(METHOD_NAME_ON_PAGE_SHOW, METHOD_ON_PAGE_SHOW);
        componentMethodInfoMap.put(METHOD_NAME_ON_PAGE_HIDE, METHOD_ON_PAGE_HIDE);
        componentMethodInfoMap.put(METHOD_NAME_ON_BACK_PRESS, METHOD_ON_BACK_PRESS);
        componentMethodInfoMap.put(METHOD_NAME_ABOUT_TO_REUSE, METHOD_ABOUT_TO_REUSE);
        componentMethodInfoMap.put(METHOD_NAME_ABOUT_TO_RECYCLE, METHOD_ABOUT_TO_RECYCLE);
        componentMethodInfoMap.put(METHOD_NAME_ON_WILL_APPLY_THEME, METHOD_ON_WILL_APPLY_THEME);
    }

    public static String methodInfo(FileType fileType, String methodName) {
        switch (fileType) {
            case Ability -> {
                if (abilityMethodInfoMap.isEmpty()) {
                    initAbilityMethodInfo();
                }
                return abilityMethodInfoMap.get(methodName);
            }
            case Component -> {
                if (componentMethodInfoMap.isEmpty()) {
                    initComponentMethodInfo();
                }
                return componentMethodInfoMap.get(methodName);
            }
        }
        return null;
    }

    public enum FileType {
        //代码插入在Ability里面
        Ability(1),
        //代码插入在当前的自定义组件里
        Component(2),
        //代码写入新文件，对于需要单独封装的类或者写在struct外面的代码
        NewFile(3);
        private final int value;

        FileType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    public enum MethodInnerPosition {
        START(0),

        END(1);

        private final int value;

        MethodInnerPosition(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }
}
